<?php
/**
 *============================================================================
 * Cornice PHP Framework & Website Builder
 * Freeware (f) 2011 by Inforo Software & Computer
 *============================================================================
 */
 
/**
 * Class contains miscellaneous utils
 * @author Nicola Dinh
 * @version 2011.04.2217
 */
class helper {

  /**
   * Default instance level constructor
   * Just decorative, should not be called due to having only static methods
   */
  public function __construct() {
    //
  }
  
  /**
   * Trim trailing & tailing characters, also extra spaces in between
   * @param string $text The text to trim
   * @return string The trimmed text
   */
  public static function trim_neat($text) {
    $text = trim($text);
    while (strpos($text,chr(32).chr(32))!==false)
      $text = str_replace(chr(32).chr(32),chr(32),$text);
    return $text;
  }
  
  /**
   * Get current skin in cookie
   * @return string The name of skin (eg. 'sunshine')
   */
  public static function get_cur_skin() {
    return system::$skin;
  }
  
  /**
   * Get current language in cookie
   * @return string The name of language (eg. 'uk','ru',...)
   */
  public static function get_cur_lang() {
    return system::$lang;
  }
  
  /**
   * Dump object to file 'dump.txt'
   * @param object $objecct The object to dump
   */
  public static function dump($object) {
    ob_start();
    var_dump($object);
    $text = ob_get_clean();
    file_put_contents("dump.txt",$text);
  }

  /**
   * Dump debug backtrace  
   */
  public static function dump_trace() {
    self::dump(debug_backtrace());
  }
  
  /**
   * Make URL with a custom param value
   * @param string $param The param in URL to add or replace
   * @param string $param_value The value to set
   * @return string The URL with param added or replaced
   */
  public static function make_url($param,$param_value) {   
    $validated_url = request::get_validated_url();
    $tokens = explode("_",$validated_url); 
    
    //convert to assoc array
    $array = array();
    for ($index=0; $index<count($tokens)/2; $index++)
      $array[$tokens[$index*2]] = $tokens[$index*2+1];
      
    //set value
    $array[$param] = $param_value;
    
    //combine into url
    $url = ""; 
    $last_key = lastkey($array);
    foreach ($array as $key=>$value) {
      $url .= $key."_".$value;
      if ($key!=$last_key)
        $url .= "_";
    }
    
    return $url; 
  }
  
  /**
   * Get URL for setting language
   * @param string $lang The language abbreviation (TLD extension)
   * @return string The relative URL with 'setlang' param
   */
  public static function get_lang_url($lang) {
    return self::make_url("setlang",$lang);
  }
  
  /**
   * Get captcha URL
   * @return string The url to captcha image
   */
  public static function get_captcha_url() {
    $skin = system::$skin;
    $module = request::get_module_name();
    return "skins/$skin/scripts/captcha.php?module=$module";
  }
  
  /**
   * Get captcha value
   * @return string The captcha value stored in session data
   */
  public static function get_captcha_value() {
    return session::get_value("captcha");
  }
  
  /**
   * Get server name (ie. 'localhost' or domain, the value configured in
   * Apache server config file.
   * @return string The server name
   */
  public static function get_server_name() {
    //SERVER_ADDR is the IP, not the name
    return strtolower($_SERVER["SERVER_NAME"]);
  }
  
  /**
   * Get script name (ie. the path & script file name after server name),
   * combines with protocol & server name will be the URL without query string.
   * If already URL-rewritten, this function returns the actual script file.
   * @return string The script path & script file name
   */
  public static function get_script_name() {
    //PHP_SELF has the same value but not popular
    //REQUEST_URI has the query string included
    return strtolower($_SERVER["SCRIPT_NAME"]);
  }
  
  /**
   * Get the path (after domain & before script file name) to requested script;
   * this path doesn't contain slashes at beginning & end;
   * this function fails if being called from somewhere else other than root
   * directory of Cornice.
   * @return string The path after domain & before file name
   */
  public static function get_script_path() {
    $script_name = self::get_script_name();
    $last_slash  = strrpos($script_name,"/");
    
    if ($last_slash===0)
      return "";
    else
      return substr($script_name,1,$last_slash-1);
  }
  
  /**
   * Get the URL to a local script; this function fails if being called from 
   * somewhere else other than root dir of Cornice; assume not using HTTPS;
   * if already URL-rewritten, this functions returns the actual URL.
   * @return string The URL to a local script, like being called from browser
   */
  public static function get_local_url($addin_path=null) {
    $server_name = self::get_server_name();
    $script_path = self::get_script_path();
    if ($addin_path!=null) {
      if (strlen($script_path)>0)
        return "http://$server_name/$script_path/$addin_path";
      else
        return "http://$server_name/$addin_path";
    }
    else {
      if (strlen($script_path)>0)
        return "http://$server_name/$script_path";
      else
        return "http://$server_name";
    }
  }//get_local_url
}